package com.ccb.sc.order.controller;

import com.ccb.sc.auth.TokenAuth;
import com.ccb.sc.auth.TokenModel;
import com.ccb.sc.order.controller.query.AppOrderCommonQueryParam;
import com.ccb.sc.order.controller.query.AppOrderQueryParam;
import com.ccb.sc.order.service.IAppOrderService;
import com.ccb.sc.order.vo.OrderCreateVo;
import com.ccb.sc.order.vo.OrderPreviewVo;
import com.ccb.sc.order.vo.OrderUpdateVo;
import com.ccb.sc.common.bind.annotation.CurrentUser;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.modules.common.vo.QueryTrackResp;
import com.ccb.sc.common.modules.order.entity.Order;
import com.ccb.sc.common.modules.order.enums.OrderStatus;
import com.ccb.sc.common.modules.order.service.IOrderService;
import com.ccb.sc.common.modules.order.vo.OrderCommentCreateVo;
import com.ccb.sc.common.modules.order.vo.OrderDetailVo;
import com.ccb.sc.common.modules.order.vo.OrderListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ken
 * @version 1.0
 * @date 2020-09-27
 */
@RestController
@TokenAuth
@RequestMapping("/app/order")
@Api(value = "订单管理", tags = "订单管理")
public class AppOrderController {

    private IAppOrderService appOrderService;
    private IOrderService orderService;

    @PostMapping("/preview")
    @ApiOperation(value = "预览订单", response = OrderPreviewVo.class)
    public ResBody<?> preview(@CurrentUser @ApiIgnore TokenModel token) {
        OrderPreviewVo preview = this.appOrderService.preview(token.getId());
        return ResBody.success(preview);
    }

    @PostMapping("/submit")
    @ApiOperation(value = "提交订单", notes = "提交订单返回订单id集合", response = String.class)
    public ResBody<?> preview(@CurrentUser @ApiIgnore TokenModel token, @RequestBody OrderCreateVo createVo) {
        String orderId = this.appOrderService.createOrder(token.getId(), createVo);
        return ResBody.success(orderId);
    }

    @PostMapping("/reSubmit")
    @ApiOperation(value = "重新提交订单", notes = "修改订单返回订单id集合", response = String.class)
    public ResBody<?> preview(@CurrentUser @ApiIgnore TokenModel token, @RequestBody OrderUpdateVo updateVo) {
        String orderId = this.appOrderService.updateOrder(token.getId(), updateVo);
        return ResBody.success(orderId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "订单列表", response = OrderListVo.class, responseContainer = "list")
    public ResBody<?> list(@CurrentUser @ApiIgnore TokenModel token, AppOrderQueryParam queryParam) {
        AppOrderCommonQueryParam commonQueryParam = new AppOrderCommonQueryParam();
        if (queryParam.getStatus() != null) {
            OrderStatus orderStatus = OrderStatus.getByValue(queryParam.getStatus());
            Assert.notNull(orderStatus, "查询订单状态错误");
            commonQueryParam.setStatus(orderStatus);
        }
        commonQueryParam.setCurrent(queryParam.getCurrent());
        commonQueryParam.setPageSize(queryParam.getPageSize());
        PageVo<OrderListVo> list = this.appOrderService.list(token.getId(), commonQueryParam);
        return ResBody.success(list);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "订单详情", response = OrderDetailVo.class)
    public ResBody<?> detail(@CurrentUser @ApiIgnore TokenModel token, @PathVariable String id) {
        OrderDetailVo detailVo = this.appOrderService.detail(token.getId(), id);
        return ResBody.success(detailVo);
    }

    @PostMapping("/rebuild")
    @ApiOperation(value = "再次购买", response = OrderPreviewVo.class)
    public ResBody<?> rebuild(@CurrentUser @ApiIgnore TokenModel token, @RequestParam String orderId) {
        OrderPreviewVo order = this.appOrderService.rebuild(token.getId(), orderId);
        return ResBody.success(order);
    }

    @PostMapping("/direct-buy")
    @ApiOperation(value = "直接购买", response = OrderPreviewVo.class)
    public ResBody<?> directBuy(@CurrentUser @ApiIgnore TokenModel token,
                                @RequestParam Long goodsSpecsId,
                                @RequestParam(defaultValue = "false") Boolean isGroupon,
                                @RequestParam(defaultValue = "1") Integer quantity) {
        OrderPreviewVo order = this.appOrderService.directBuy(token.getId(), goodsSpecsId, quantity, isGroupon);
        return ResBody.success(order);
    }

    @PostMapping("/receive")
    @ApiOperation(value = "确认收货")
    public ResBody<?> receive(@CurrentUser TokenModel tokenModel, @RequestParam String orderId) {
        Order order = this.orderService.getById(orderId);
        Assert.isTrue(order != null && order.getMemberId().equals(tokenModel.getId()), "订单不存在");
        this.orderService.confirmReceive(orderId);
        return ResBody.success();
    }

    @PostMapping("/comment")
    @ApiOperation(value = "评价")
    public ResBody<?> comment(@CurrentUser TokenModel tokenModel,
                              @Validated @RequestBody OrderCommentCreateVo createVo,
                              @ApiIgnore HttpServletRequest request) {
        this.appOrderService.comment(tokenModel.getId(), createVo, request);
        return ResBody.success();
    }

    @PostMapping("/cancel/{orderId}")
    public ResBody<?> cancel(@CurrentUser TokenModel tokenModel, @PathVariable String orderId) {
        Order order = this.orderService.getById(orderId);
        Assert.isTrue(order != null && order.getMemberId().equals(tokenModel.getId()), "订单不存在");
        this.orderService.cancel(orderId);
        return ResBody.success();
    }

    @GetMapping("/trace/{orderId}")
    @ApiOperation(value = "订单物流信息", response = QueryTrackResp.class)
    public ResBody<?> trace(@CurrentUser @ApiIgnore TokenModel token, @PathVariable String orderId) {
        Order order = this.orderService.getById(orderId);
        Assert.isTrue(token.getId().equals(order.getMemberId()), "订单不存在");
        QueryTrackResp queryTrackResp = this.orderService.orderTrace(orderId);
        return ResBody.success(queryTrackResp);
    }

    @Autowired
    public void setAppOrderService(IAppOrderService appOrderService) {
        this.appOrderService = appOrderService;
    }

    @Autowired
    public void setOrderService(IOrderService orderService) {
        this.orderService = orderService;
    }
}
